package com.stu.mapper;

import com.github.pagehelper.Page;
import com.stu.Dto.JobApplicationDTO;
import com.stu.Dto.JobInformationDTO;
import com.stu.Dto.JobPageQueryDTO;
import com.stu.annotation.AutoFill;
import com.stu.enumeration.OperationType;
import com.stu.pojo.JobApplication;
import com.stu.pojo.JobWorking;
import com.stu.pojo.JobInformation;
import com.stu.vo.JobApplicationVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JobInformationMapper {

    /**
     * 查询工作信息
     *
     * @param jobPageQueryDTO
     * @return
     */
    Page<JobInformation> pageQuery(JobPageQueryDTO jobPageQueryDTO);

    /**
     * 查询该学生是否申请该项工作
     * @return
     */
    @Select("select id from job_application where stu_id = #{currentId} and job_id = #{jobId}")
    Integer selectIdByCurrentId(Long currentId, Integer jobId);

    /**
     * 添加工作申请信息
     * @param jobApplicationDTO
     */
    @Insert("insert into job_application(stu_id, job_name, job_id, application_time) " +
            "VALUES (#{stuId},#{jobName},#{jobId}, #{applicationTime})")
    void insertJobApplication(JobApplicationDTO jobApplicationDTO);

    /**
     * 对job信息表的申请人数进行修改
     * @param jobId
     */
    @Update("update job_information set application_number = " +
            "(SELECT COUNT(*) FROM job_application WHERE job_id = #{jobId})" +
            "where id = #{jobId}")
    void updateJobInformation(Integer jobId);

    /**
     *批量确认学生申请
     * @param job
     */
    @Insert("insert into job_working(stu_id, job_name, job_id, work_time, confirm_admin) " +
            "VALUES (#{stuId},#{jobName},#{jobId}, #{workTime}, #{confirmAdmin})")
    void insertJobWorking(JobWorking job);

    /**
     * 将该学生加工作入职表
     * @param jobId
     */
    @Update("update job_information set working_number = " +
            "(SELECT COUNT(*) FROM job_working WHERE job_id = #{jobId})" +
            "where id = #{jobId}")
    void updateJobWorking(Integer jobId);

    @AutoFill(value = OperationType.INSERT)
    void insertJobInformation(JobInformation jobInformation);

    @AutoFill(value = OperationType.UPDATE)
    void updateAllJobInformation(JobInformationDTO jobInformationDTO);

    /**
     * 删除工作信息表
     * @param ids
     */
    void deleteJobInformation(List<Integer> ids);

    void deleteJobApplication(List<Integer> ids);

    void deleteJobWorking(List<Integer> ids);

    /**
     * 查询已经申请的工作
     * @param currentId
     */
    @Select("select * from job_application where stu_id = #{currentId}")
    List<JobApplication> selectApplicationById(Long currentId);

    @Select("select * from job_information where id = #{jobId}")
    JobInformation selectInformation(Integer jobId);

    /**
     * 查询所有勤工俭学申请
     * @return
     */
    @Select("select * from job_application")
    List<JobApplication> selectApplicationAll();
}
